package domain;

import Commands.Command;
import Commands.CommandFactory;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;
import beans.Subject;

public class WebServlet
  extends HttpServlet
{
  private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
  Controller controller = new Controller();
  CommandFactory comFac = new CommandFactory();
  Command command;
  
  public void init(ServletConfig config)
    throws ServletException
  {
    super.init(config);
    command = null;
  }

  public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setContentType(CONTENT_TYPE);
      command = comFac.getCommand(request);
      command.init(comFac, controller);
      command.execute(request, response); 
    dispatch(request, response, command.getNextJspUrl()); 
  }

  public void dispatch(HttpServletRequest request, 
                       HttpServletResponse response, String target)
    throws ServletException, IOException
  {
    RequestDispatcher dispatcher = 
      getServletContext().getRequestDispatcher("/" + target);
    dispatcher.forward(request, response);
  }
}
